package net.sk.china.city.talk.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sk.china.common.request.PageParamRequest;
import net.sk.china.common.response.CommonPage;
import net.sk.china.common.response.CommonResult;
import net.sk.china.city.talk.model.city.City;
import net.sk.china.city.talk.request.city.CityAddRequest;
import net.sk.china.city.talk.request.city.CityRequest;
import net.sk.china.city.talk.request.city.CityRestPasswordRequest;
import net.sk.china.city.talk.request.city.CityUpdateRequest;
import net.sk.china.city.talk.response.city.CityResponse;
import net.sk.china.city.talk.service.city.CityService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * +----------------------------------------------------------------------
 * | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2018-2024 聊来All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * +----------------------------------------------------------------------
 * | @Author: 聊来 <18970881148@qq.com>
 * +----------------------------------------------------------------------
 * | DateTime: 2024/6/11 15:25
 * +----------------------------------------------------------------------
 */

@RestController
@RequestMapping("city/")
@Api(tags = "分站相关")
public class CityController {

    private final CityService cityService;

    public CityController(CityService cityService) {
        this.cityService = cityService;
    }


    @PreAuthorize("hasAuthority('platform:city:page:list')")
    @ApiOperation(value = "列表")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public CommonResult<CommonPage<CityResponse>> getList(@Validated CityRequest request, PageParamRequest page) {
        return CommonResult.success(CommonPage.restPage(cityService.getList(request, page)));
    }

    @PreAuthorize("hasAuthority('platform:city:detail')")
    @ApiOperation(value = "详情")
    @RequestMapping(value = "detail/{id}", method = RequestMethod.GET)
    public CommonResult<City> getDetail(@PathVariable(value = "id") Integer id) { return CommonResult.success(cityService.getDetail(id)); }

    @PreAuthorize("hasAuthority('platform:city:status')")
    @ApiOperation(value = "更新状态")
    @RequestMapping(value = "updateStatus", method = RequestMethod.PUT)
    @ApiImplicitParams({
            @ApiImplicitParam(name="id", value="数据ID", dataTypeClass = Integer.class),
            @ApiImplicitParam(name="status", value="status 状态 true:开启 false:关闭", dataTypeClass = Boolean.class)
    })
    public CommonResult<String> updateStatus(@RequestParam(value = "id") Integer id, @RequestParam(value = "status") Boolean status) {
        if (!cityService.updateStatus(id, status)) {
            return CommonResult.failed("操作失败");
        }

        return CommonResult.success("操作成功");
    }

    @PreAuthorize("hasAuthority('platform:city:reset:password')")
    @ApiOperation(value = "重置")
    @RequestMapping(value = "restPassword", method = RequestMethod.PUT)
    public CommonResult<String> restPassword(@RequestBody @Validated CityRestPasswordRequest request) {
        if (!cityService.restPassword(request)) {
            return CommonResult.failed("重置密码失败");
        }

        return CommonResult.success("重置密码成功");
    }

    @PreAuthorize("hasAuthority('platform:city:add')")
    @ApiOperation(value = "添加分站超管")
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public CommonResult<String> add(@RequestBody @Validated CityAddRequest request) {
        if (!cityService.add(request)) {
            return CommonResult.failed("添加失败");
        }

        return CommonResult.success("添加成功");
    }

    @PreAuthorize("hasAuthority('platform:city:update')")
    @ApiOperation(value = "更新分站超管")
    @RequestMapping(value = "update", method = RequestMethod.PUT)
    public CommonResult<String> update(@RequestBody @Validated CityUpdateRequest request) {
        if (!cityService.update(request)) {
            return CommonResult.failed("更新失败");
        }

        return CommonResult.success("更新成功");
    }
}
